package com.atguigu.gmall.controller;

import com.atguigu.gmall.bean.ProductStats;
import com.atguigu.gmall.service.ProductStatsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/sugar")
public class SugarController {

    @Autowired
    ProductStatsService productStatsService;

    /**
     * {
     * "status": 0,
     * "data": 1201079.1342345553
     * }
     *
     * @param date 哪一天
     * @return 当天的总金额
     */
    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(value = "date", defaultValue = "0") Integer date) {

        if (date == 0) {
            date = now();
        }
        BigDecimal gmv = productStatsService.getGMV(date);
        return "{ \"status\": 0, \"data\": " + gmv + "}";
    }

    /**
     * {
     * "status": 0,
     * "data": {
     * "categories": [
     * "苹果",
     * "三星",
     * "华为",
     * "oppo",
     * "vivo",
     * "小米23"
     * ],
     * "series": [
     * {
     * "name": "手机品牌",
     * "data": [
     * 7229,
     * 8918,
     * 9212,
     * 6649,
     * 8324,
     * 5884
     * ]
     * }
     * ]
     * }
     * }
     *
     * @param date  哪一天
     * @param limit 取前几
     * @return 当天的品牌交易金额排行
     */
    @RequestMapping("/trademark")
    public String getTM(@RequestParam(value = "date", defaultValue = "0") Integer date,
                        @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        if (date == 0) {
            date = now();
        }
        // 获取查询到的数据
        List<ProductStats> productStatsTMList = productStatsService.getTM(date, limit);

        // 将品牌名称和交易金额取出来
        List<String> tmNameList = new ArrayList<>();
        List<BigDecimal> orderAmountList = new ArrayList<>();
        for (ProductStats productStats : productStatsTMList) {
            tmNameList.add(productStats.getTm_name());
            orderAmountList.add(productStats.getOrder_amount());
        }
        return "{\"status\": 0, " +
                "\"data\": {" +
                "\"categories\": [" +
                "\"" + StringUtils.join(tmNameList, "\",\"") + "\"], " +
                "\"series\": [" +
                "{\"name\": \"品牌\", " +
                "\"data\": [" +
                "" + StringUtils.join(orderAmountList, ",") + "]}]}" +
                "}";
    }

    /**
     * {
     * "status": 0,
     * "data": [
     * {
     * "name": "windows phone",
     * "value": 29
     * },
     * {
     * "name": "Black berry",
     * "value": 3
     * }
     * ]
     * }
     *
     * @param date  哪一天
     * @param limit 取前几
     * @return 当天的品类交易金额排行
     */
    @RequestMapping("/category3")
    public String getCategory3(@RequestParam(value = "date", defaultValue = "0") Integer date,
                               @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        if (date == 0) date = now();
        List<ProductStats> productStatsCategoryList = productStatsService.getCategory3(date, limit);

        StringBuilder json = new StringBuilder("{\"status\": 0, \"data\": [");

        int i = 0;
        for (ProductStats productStats : productStatsCategoryList) {
            json.append("{ \"name\": \" " + productStats.getCategory3_name() + "\",\"value\": " + productStats.getOrder_amount() + "}");
            if (i++ < productStatsCategoryList.size() - 1) {
                json.append(",");
            }
        }

        json.append("]}");

        return json.toString();
    }

    /**
     * {
     *   "status": 0,
     *   "data": {
     *     "columns": [
     *       {
     *         "name": "sku",
     *         "id": "subsys"
     *       },
     *       {
     *         "name": "spu",
     *         "id": "module"
     *       },
     *       {
     *         "name": "trademark",
     *         "id": "business"
     *       },
     *       {
     *         "name": "category3",
     *         "id": "name"
     *       },
     *       {
     *         "name": "销售额",
     *         "id": "value",
     *         "accuracy": 2
     *       }
     *     ],
     *     "rows": [
     *       {
     *         "subsys": "北京总部",
     *         "module": "市场部",
     *         "business": "第一小分队",
     *         "name": "张三",
     *         "value": 6672389
     *       },
     *       {
     *         "subsys": "北京总部",
     *         "module": "市场部",
     *         "business": "第一小分队",
     *         "name": "李四",
     *         "value": 7673456.78
     *       }
     *     ]
     *   }
     * }
     * @param date 哪一天
     * @param limit 取前几
     * @return 当天的商品交易排行
     */
    @RequestMapping("/spu")
    public String getSpu(@RequestParam(value = "date", defaultValue = "0") Integer date,
                         @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        List<ProductStats> spuList = productStatsService.getSpu(date, limit);

        StringBuilder json = new StringBuilder("{\"status\": 0, \"data\": {\"columns\": [" +
                "{\"name\": \"商品\",\"id\": \"spu\"}," +
                "{\"name\": \"交易额\",\"id\": \"order_amount\"}," +
                "{\"name\": \"订单数\",\"id\": \"order_ct\"}," +
                "\"rows\":[");

        int i =0;
        for (ProductStats productStats : spuList) {
            json.append("{\"spu\": \""+productStats.getSpu_name()+"\"," +
                    "\"order_amount\": \""+productStats.getOrder_amount()+"\"," +
                    "\"order_ct\": \""+productStats.getOrder_ct()+"\"}");
            if (i++ < spuList.size() - 1) {
                json.append(",");
            }
        }
        json.append("]}}");

        return json.toString();
    }

    // 获取当前时间
    private Integer now() {
        String yyyyMMdd = DateFormatUtils.format(new Date(), "yyyyMMdd");
        return Integer.valueOf(yyyyMMdd);
    }

}
